import 'package:flutter/material.dart';
import 'package:whale/helper/utils/um/event.dart';
import 'package:whale/helper/utils/um/page.dart';
import 'package:whale/helper/utils/um/um_man.dart';

import '../../../helper/global/user/logic.dart';
import '../../../helper/utils/httputils/http_man.dart';
import '../../../helper/utils/httputils/urlpath.dart';
import '../../../helper/utils/key_val_model.dart';
import '../../../helper/utils/util.dart';
import 'index.dart';

class LoginController extends GetxController {
  LoginController();

  final state = LoginState();

  void actionLogin() {
    if (state.account.isEmpty) {
      Hud.toast('请输入账号');
      state.accountError = '请输入账号';
      update();
      return;
    }
    if (state.pwdCtrl.text.isEmpty) {
      var placeholder = state.isPwdLogin ? '请输入密码' : '请输入验证码';
      Hud.toast(placeholder);
      state.pwdError = placeholder;
      update();
      return;
    }
    login(state.account, state.pwdCtrl.text);
  }

  void login(String account, String pwd) async {
    // UmMan()
    //     .onEvent(event: UmEvent.login, param: {'account': account, 'pwd': pwd});
    var res = await HttpMan.instance.post(UrlPath.login, args: {
      "appCode": "jx-clivil-servant-assessment",
      "loginName": account,
      "loginPassword": pwd,
      "loginType": state.isPwdLogin ? '1' : '2',
      "platType": 1,
      "resolution": "${Get.height}*${Get.width}",
      "screenSize": "${Get.height}*${Get.width}",
      'smCode': state.isPwdLogin ? '' : pwd,
      "userOs": GetPlatform.isAndroid ? "Andriod" : "IOS",
    });
    if (res.isSuccess) {
      UserController.instance.saveLogin(account: account, pwd: pwd);
      await UserController.instance.loginSuccess(res.data);
      Get.offAllNamed(RouterPath.main);
    } else {
      Hud.toast(res.msg);
    }
  }

  Future<bool> sendCode() async {
    if (state.account.isEmpty) {
      Hud.toast('请输入账号');
      state.accountError = '请输入账号';
      update();
      return false;
    }
    var res = await HttpMan.instance.post(UrlPath.sendCode, args: {
      "oneParam": state.account,
      "sendType": 1,
      "check": '1',
      'tenantId': '',
    });
    if (res.isSuccess) {
      return true;
    } else {
      Hud.toast(res.msg);
      return false;
    }
  }

  void toForgetPwd() {
    Get.toNamed(RouterPath.forgot_new_pwd);
  }

  void wxLogin() {}

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.accNode = FocusNode();
    state.pwdNode = FocusNode();

    state.pwdCtrl = TextEditingController();
    // login('wangchongyang', 'topnet');
    // login('bumenfzr1', 'topnet@123');
    // login('15638540357', 'topnet@123');
    // login('fenguanfzr1', 'topnet@123');
    // login('guojing', 'topnet');
    KeyValModel? m = Get.arguments;
    if (m != null) {
      login(m.key, m.val!);
    }
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    // Popup.show(
    //   const ChangeRoleEntryView(),
    //   position: PopupPosition.bottom,
    // );
    // UmMan().onPageStart(UmPage.login);
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    // UmMan().onPageEnd(UmPage.login);
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
    state.pwdCtrl.dispose();
  }
}
